In Svelte, stores are reactive objects used to manage state across components. There are three main types of stores: writable, readable, and derived. Each serves a different purpose.
Writable stores allow both reading and updating values. You can use set to assign a new value or update to modify it based on the current value.
Readable stores are read-only from the component perspective. Their value is updated internally, often via side effects like timers, API calls, or subscriptions. They are ideal for state that should not be directly modified by components.
Derived stores compute their value based on one or more other stores. Whenever the source stores change, the derived store automatically updates.
Writable stores: Can be read and updated directly.
Readable stores: Read-only externally; updated internally or via side effects.
Derived stores: Values depend on other stores and update automatically when dependencies change.
Use $store syntax in Svelte components to auto-subscribe to any store.